package com.example.testndk;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tv;
    ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv_app_package_name);
        iv = (ImageView) findViewById(R.id.iv);


        //灰图
        long current = System.currentTimeMillis();
// 先打开图像并读取像素
        Bitmap img1 = ((BitmapDrawable) ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_launcher)).getBitmap();
        int w = img1.getWidth();
        int h = img1.getHeight();
        int[] pix = new int[w * h];
        img1.getPixels(pix, 0, w, 0, 0, w, h);
// 通过C++把彩色像素转为灰度像素
        int[] resultInt = JNIDynamicUtils.grayPic(pix, w, h);
        Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
        long ndkTime = System.currentTimeMillis() - current;
// 显示灰度图
        iv.setImageBitmap(resultImg);

        tv.setText(tv.getText().toString() + "\n" + "--->w:" + img1.getWidth() + ",h:"
                + img1.getHeight() + " NDK  TIME: " + ndkTime+ " ms");


        //异常
//        dynamicJniGetStrLength(null);
//        dynamicJniGetStrLength("hello world");
//        dynamicJniGetStrLength("hello");

        //计算
        //tv.setText("--->string from static jni: " + JNIDynamicUtils.getSumFromJNI(3, 4));

        //动态
        // tv.setText("packageName: " + JNIDynamicUtils.getHelloStringFromJNI());

        //静态
        //tv.setText("packageName: " + JniTest.getPackname(MainActivity.this));
    }
//    private void dynamicJniGetStrLength(String str) {
//        // tvInfo为界面上的一个TextView控件
//        try {
//            tv.setText(tv.getText().toString() + "\n" + "--->dynamic jni: " + JNIDynamicUtils.getStrLength(str));
//        } catch (NullPointerException e) {
//            tv.setText(tv.getText().toString() + "\n" + "--->dynamic jni:" + e);
//        } catch (IllegalArgumentException e) {
//            tv.setText(tv.getText().toString() + "\n" + "--->dynamic jni:" + e);
//        }
//    }
}
